﻿using DALFactory;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public abstract class BaseService<T> where T : class, new()
    {
        protected IDbSession dbSession;
        protected IBaseDal<T> CurrentDal;

        public BaseService()
        {
            dbSession = DbSessionFactory.GetCurrentDbSession();
            SetCurrentDal();
        }


        //强迫子类给父类的一个属性赋值
        public abstract void SetCurrentDal();

        public T Add(T entity)
        {
            CurrentDal.Add(entity);
            dbSession.SaveChanges();
            return entity;
        }

        public bool Update(T entity)
        {
            CurrentDal.Update(entity);
            return dbSession.SaveChanges() > 0;
        }

        public bool Update(T entity, string[] arr)
        {
            CurrentDal.Update(entity, arr);
            return dbSession.SaveChanges() > 0;
        }

        public bool Delete(T entity)
        {
            CurrentDal.Delete(entity);
            return dbSession.SaveChanges() > 0;
        }

        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.LoadEntities(whereLambda);
        }

    }
}
